package com.xbongbong.paas.script.controller;


import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.pro.config.ProBaseConfig;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.flowbill.dto.FlowBillInsertDTO;
import com.xbongbong.pro.script.pojo.dto.ResetFlowBillDTO;
import com.xbongbong.saas.model.BatchFlowBillModel;
import com.xbongbong.saas.model.BatchSurplusRecordModel;
import com.xbongbong.saas.model.StockFlowBillModel;
import com.xbongbong.saas.model.StockSurplusRecordModel;
import com.xbongbong.saas.service.BatchFlowBillService;
import com.xbongbong.saas.service.StockFlowBillService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * 流水脚本
 *
 * @author xinpeng.jiang
 * @version V1.0
 * @date 2020/12/4 10:39
 * @since V1.0
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/script/flowBill")
public class FlowBillController {
    private static final Logger LOG = LoggerFactory.getLogger(FlowBillController.class);
    @Resource
    private ProBaseConfig proBaseConfig;
    @Resource
    private StockFlowBillModel stockFlowBillModel;
    @Resource
    private StockSurplusRecordModel stockSurplusRecordModel;
    @Resource
    private StockFlowBillService stockFlowBillService;
    @Resource
    private BatchFlowBillModel batchFlowBillModel;
    @Resource
    private BatchSurplusRecordModel batchSurplusRecordModel;
    @Resource
    private BatchFlowBillService batchFlowBillService;

    /**
     * 重置流水脚本，传入corpid和需要重置的时间
     *
     * @param resetFlowBillDTO
     * @param br
     * @return java.lang.String
     * @throws XbbException
     * @author xinpeng.jiang
     * @date 2021/3/22 16:11
     * @version 1.0
     */
    @RequestMapping(value = "/reset", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public void reset(@RequestBody @Valid ResetFlowBillDTO resetFlowBillDTO, BindingResult br) throws Exception {
        if (!proBaseConfig.getXbbCode().equals(resetFlowBillDTO.getXbbCode())) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100006, SystemErrorCodeEnum.API_ERROR_100006.getMsg());
        }
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            LOG.error(errMsg);
        } else {
            long start = DateTimeUtil.getInt();
            String corpid = resetFlowBillDTO.getCorpid();
            FlowBillInsertDTO flowBillInsertDTO = new FlowBillInsertDTO();
            BeanUtil.copyProperties(resetFlowBillDTO, flowBillInsertDTO);
            // 先删除后添加
            stockFlowBillModel.deleteAllByCorpId(corpid);
            stockSurplusRecordModel.deleteAllByCorpId(corpid);
            stockFlowBillService.insertMonthBeforeStockFlow(flowBillInsertDTO);
            batchFlowBillModel.deleteAllByCorpId(corpid);
            batchSurplusRecordModel.deleteAllByCorpId(corpid);
            batchFlowBillService.insertMonthBeforeBatchFlow(flowBillInsertDTO);

            LOG.warn("consume:{}s", (DateTimeUtil.getInt() - start));
            LOG.warn("success");
        }
    }

}
